// 为了方便管理请求接口，所有的请求都以异步 action 的方式进行封装
import http from '@/utils/http'
import { setTokenInfo } from '@/utils/storage'
import store, { AppDispatch } from '..'
import { Profile, UserAction } from '../reducer/user'

/**
 * 发送短信验证码
 * @param mobile 手机号
 * @returns
 */
export const sendCodes = (mobile: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get(`/v1_0/sms/codes/${mobile}`)
    console.log(res)
  }
}

/**
 * 用户认证（登录注册）
 * @param {object} { mobile, code }
 * @returns
 */
export const Authorizations = ({ mobile, code }: { mobile: string; code: string }) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.post(`/v1_0/authorizations`, { mobile, code })
    // console.log(`用户认证（登录注册）`, res)
    // 保存到 redux 中
    dispatch({ type: 'user/saveAuth', payload: res.data })
    // 保存到 本地存储
    setTokenInfo(res.data)
  }
}

/**
 * 获取用户自己信息
 * @returns
 */
export const getUserInfo = () => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get(`/v1_0/user`)
    // console.log(`/v1_0/user`, res)
    dispatch({ type: 'user/saveUser', payload: res.data })
  }
}

/**
 * 获取用户个人资料
 * @returns
 */
export const getUserProfile = () => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get(`/v1_0/user/profile`)
    // console.log(`/v1_0/user/profile`, res)
    dispatch({ type: 'user/saveProfile', payload: res.data })
  }
}

/**
 * 编辑用户照片资料（头像、身份证照片）
 * @param fd FormData
 * @returns
 */
export const editUserPhoto = (fd: FormData) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.patch(`/v1_0/user/photo`, fd)
    console.log(`/v1_0/user/photo`, res)
    // 更新仓库用户数据
    dispatch({ type: 'user/updateProfile', payload: res.data })
  }
}

/**
 * 编辑用户个人资料
 * @param data Profile 其中一个
 * @returns (dispatch: AppDispatch) => Promise<void>
 */
export const editUserProfile = (data: Partial<Profile>) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.patch(`/v1_0/user/profile`, data)
    console.log(`/v1_0/user/profile`, res)
    // 更新仓库用户数据
    dispatch({ type: 'user/updateProfile', payload: data })
  }
}

/**
 * 清空用户认证信息
 * @param data Profile 其中一个
 * @returns (dispatch: AppDispatch) => Promise<void>
 */
export const clearAuth = (): UserAction => {
  return { type: 'user/clearAuth' }
}

/**
 * 关注用户和取消关注用户
 * @param target 目标用户（被取消关注的用户id）
 * @returns (dispatch: AppDispatch) => Promise<void>
 */
export const changeUserFollowings = (target: string) => {
  return async (dispatch: AppDispatch) => {
    const { detail } = store.getState().article
    if (detail.is_followed === false) {
      // 情况1：关注
      await http.post(`/v1_0/user/followings`, { target })
      dispatch({ type: 'article/changeDetail', payload: { is_followed: !detail.is_followed } })
    } else {
      // 情况2：取消关注
      await http.delete(`/v1_0/user/followings/${target}`)
      dispatch({ type: 'article/changeDetail', payload: { is_followed: !detail.is_followed } })
    }
  }
}
